package indi.kider.server.employee.service;

import indi.kider.common.constant.EmployeeRoleCons;
import indi.kider.common.model.employee.Employee;
import indi.kider.common.model.employee.Enterprise;
import indi.kider.common.model.apartment.Community;
import indi.kider.common.model.employee.Waiter;
import indi.kider.server.apartment.mapper.CommunityMapper;
import indi.kider.server.employee.mapper.EmployeeMapper;
import indi.kider.server.employee.mapper.EnterpriseMapper;
import indi.kider.server.employee.mapper.WaiterMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import pers.vankid.common.model.base.CommonResponse;
import pers.vankid.common.utils.page.PageRequest;
import pers.vankid.common.utils.page.PageResponse;
import pers.vankid.common.utils.page.PageUtil;

import java.util.Date;
import java.util.List;

/**
 * Created by shixj on 2018/4/26.
 */
@Service
@Slf4j
public class EnterpriseServiceImpl implements EnterpriseService {
    @Autowired
    private EnterpriseMapper enterpriseMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private WaiterMapper waiterMapper;

    @Autowired
    private CommunityMapper communityMapper;

    /**
     * 根据id获取
     *
     * @param id
     * @return
     */
    @Override
    public Enterprise getEnterpriseById(Long id) {
        return enterpriseMapper.selectByPrimaryKey(id);
    }

    /**
     * 获取分页查询
     *
     * @param request
     * @return
     */
    @Override
    public PageResponse<List<Enterprise>> getEnterprisePages(PageRequest<Enterprise> request) {
        PageUtil.start(request);
        return PageUtil.getPageResponse(enterpriseMapper.getEnterpriseList(request.getQuery()));
    }

    /**
     * 获取分页查询
     *
     * @param request
     * @return
     */
    @Override
    public PageResponse<List<Enterprise>> getEnterprisePages(PageRequest<Enterprise> request,Employee employee) {
        Enterprise enterprise = request.getQuery();
        //  1 公寓出租方 2公寓入住方
        setEnterprise(enterprise,employee);
        PageUtil.start(request);
        return PageUtil.getPageResponse(enterpriseMapper.getEnterpriseList(enterprise));
    }
    /**
     * 不分页查询
     *
     * @param enterprise
     * @return
     */
    @Override
    public List<Enterprise> getEnterprises(Enterprise enterprise,Employee employee) {
        setEnterprise(enterprise,employee);
        return enterpriseMapper.getEnterpriseList(enterprise);
    }


    /**
     * @return
     */
    public List<Enterprise> getEnterprisesByCommunityId(Community community) {
        return enterpriseMapper.getEnterprisesByCommunityId(community);
    }

    public List<Enterprise> getEnterprisesByHouseId(Long houseId) {
        return enterpriseMapper.getEnterprisesByHouseId(houseId);
    }

    private void setEnterprise(Enterprise enterprise,Employee employee){
        //  1 公寓出租方 2公寓入住方
        if(employee.getRoles().equals(EmployeeRoleCons.ADMIN)){
            enterprise.setApartmentType("1");
        }else if(employee.getRoles().equals(EmployeeRoleCons.SUPERENTERPRISE)){
            enterprise.setEnId(employee.getEnterpriseId());
            enterprise.setApartmentType("2");
        }
    }

    /**
     * 新增
     *
     * @param enterprise
     * @return
     */
    @Transactional
    @Override
    public Enterprise add(Enterprise enterprise) {
        if (enterprise != null) {
            if (enterprise.getId() == null || enterprise.getId().equals(0L)) {
                enterprise.setId(null);
                enterprise.setInsertTime(new Date());
                enterpriseMapper.insert(enterprise);
            }
        }
        return enterprise;
    }

    /**
     * 更新
     *
     * @param enterprise
     * @return
     */
    @Transactional
    @Override
    public Enterprise modify(Enterprise enterprise) {
        if (enterprise != null && enterprise.getId() != null && enterprise.getId() > 0L)
            enterpriseMapper.updateByPrimaryKeySelective(enterprise);
        return enterprise;
    }

    /**
     * 删除
     *
     * @param id
     */
    @Transactional
    @Override
    public CommonResponse delById(Long id) {
        Employee employee = new Employee();
        employee.setEnterpriseId(id);
        int cnt = employeeMapper.selectCount(employee);
        if(cnt>0){
            return CommonResponse.buildBadRequestResponse("存在员工信息,请先删除员工!");
        }
        Community community = new Community();
        community.setEnterpriseId(id);
        cnt = communityMapper.selectCount(community);
        if(cnt>0){
            return CommonResponse.buildBadRequestResponse("存在公寓信息,请先删除公寓!");
        }
        Waiter waiter = new Waiter();
        waiter.setEnterpriseId(id);
        cnt = waiterMapper.selectCount(waiter);
        if(cnt>0){
            return CommonResponse.buildBadRequestResponse("存在工人信息,请先删除工人!");
        }
        enterpriseMapper.deleteByPrimaryKey(id);
        return CommonResponse.buildDelSuccessResponse("删除企业成功!");
    }
}
